package org.androidnotebook;

import android.app.Dialog;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class PageListActivity extends ListActivity {
	private static final int MENU_CREATE_PAGE = Menu.FIRST;
	private static final int DELETE_PAGE_ID = Menu.FIRST + 1;
	private static final int RENAME_PAGE_ID = Menu.FIRST + 2;

	
	static final int PAGE_NAME_DIALOG_ID = 0;
	static final int PAGE_RENAME_DIALOG_ID = 1;

	private PageDbAdapter mDbHelper;
	private long mBookId;
	private long mItemSelectedID;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_list);
		mDbHelper = new PageDbAdapter(this);
		mDbHelper.open();
		
        Bundle extras = getIntent().getExtras();
        if (extras!=null) {
        	mBookId = extras.getLong(PageDbAdapter.KEY_BOOKID);
        	setTitle(getString(R.string.app_name)+" - "+extras.getString(BookDbAdapter.KEY_TITLE));
        }
		fillData();
		registerForContextMenu(getListView());
	}

	private void fillData() {
		Cursor pagesCursor = mDbHelper.fetchAllPagesforBook(mBookId);
		startManagingCursor(pagesCursor);

		// Create an array to specify the fields we want to display in the list
		// (only TITLE)
		String[] from = new String[] { PageDbAdapter.KEY_TITLE };

		// and an array of the fields we want to bind those fields to (in this
		// case just page_title)
		int[] to = new int[] { R.id.page_title };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter pages = new SimpleCursorAdapter(this,
				R.layout.page_row, pagesCursor, from, to);
		setListAdapter(pages);
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_CREATE_PAGE, 0, R.string.menu_create_page);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_CREATE_PAGE:
			showDialog(PAGE_NAME_DIALOG_ID);
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, RENAME_PAGE_ID, 0, R.string.menu_rename_page);
		menu.add(0, DELETE_PAGE_ID, 1, R.string.menu_delete_page);
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		mItemSelectedID=info.id;
		
		switch (item.getItemId()) {
		case RENAME_PAGE_ID:
			showDialog(PAGE_RENAME_DIALOG_ID);
			return true;
		case DELETE_PAGE_ID:
			mDbHelper.deletePage(mItemSelectedID);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	protected Dialog onCreateDialog(int id) {
		Dialog dialog;

		switch (id) {
		case PAGE_NAME_DIALOG_ID:
			dialog = new Dialog(this);
			dialog.setContentView(R.layout.page_title_dialog);
			dialog.setTitle(R.string.page_name_dialog_title);

			Button nameOkButton = (Button) dialog.findViewById(R.id.ok);
			nameOkButton.setOnClickListener(new NameOKListener(dialog));

			Button nameCancelButton = (Button) dialog.findViewById(R.id.cancel);
			nameCancelButton.setOnClickListener(new CancelListener(dialog));
			break;
		case PAGE_RENAME_DIALOG_ID:
			dialog = new Dialog(this);
			dialog.setContentView(R.layout.page_title_dialog);
			dialog.setTitle(R.string.page_rename_dialog_title);

			Button renameOkButton = (Button) dialog.findViewById(R.id.ok);
			renameOkButton.setOnClickListener(new RenameOKListener(dialog));

			Button renameCancelButton = (Button) dialog.findViewById(R.id.cancel);
			renameCancelButton.setOnClickListener(new CancelListener(dialog));
			break;
		default:
			dialog = null;
		}
		return dialog;
	}
	
	protected class CancelListener implements OnClickListener {

		private Dialog mDialog;

		public CancelListener(Dialog dialog) {
			mDialog = dialog;
		}

		public void onClick(View v) {
			mDialog.dismiss();
		}
	}

	protected class NameOKListener implements OnClickListener {

		private Dialog mDialog;

		public NameOKListener(Dialog dialog) {
			mDialog = dialog;
		}

		public void onClick(View v) {
			EditText edittext = (EditText) mDialog	.findViewById(R.id.page_title);

			mDbHelper.createPage(edittext.getText().toString(),mBookId);
			fillData();

			mDialog.dismiss();
		}
	}
	
	protected class RenameOKListener implements OnClickListener {

		private Dialog mDialog;

		public RenameOKListener(Dialog dialog) {
			mDialog = dialog;
		}

		public void onClick(View v) {
			EditText edittext = (EditText) mDialog.findViewById(R.id.page_title);
			mDbHelper.updatePageTitle(mItemSelectedID,edittext.getText().toString());
			fillData();
			mDialog.dismiss();
		}
	}

	protected void onPrepareDialog(int id,Dialog dialog) {
		super.onPrepareDialog(id, dialog);
		EditText edittext = (EditText) dialog.findViewById(R.id.page_title);
		switch (id) {
		case PAGE_NAME_DIALOG_ID:
			edittext.setText("");
			break;
		case PAGE_RENAME_DIALOG_ID:
			Cursor cursor = mDbHelper.fetchPage(mItemSelectedID);
			String title=cursor.getString(cursor.getColumnIndexOrThrow(BookDbAdapter.KEY_TITLE));
			edittext.setText(title);
			break;
		}
		
	}

}
